from google.appengine.ext import db

class TestSuite(db.Model):
    name = db.StringProperty()
    project = db.StringProperty()
    
    @classmethod
    def select_list(cls):
        q = cls.all()
        all_list = []
        for testsuite in q.fetch(100):
            all_list.append((str(testsuite.key()), testsuite.name))
        return all_list
        
    
class TestCase(db.Model):
    id = db.IntegerProperty(required=True, default=0)
    enabled = db.BooleanProperty(default=True)
    tags = db.ListProperty(db.Category)
    testsuite = db.ReferenceProperty(TestSuite)
    summary = db.StringProperty()
    instruction = db.TextProperty()
    expected_result = db.TextProperty()
    
    @classmethod
    def get_by_id(cls, id):
        return TestCase.gql("WHERE id = :1", int(id)).fetch(1)[0]

class TestRun(db.Model):
    name = db.StringProperty()
    testsuites = db.ListProperty(db.Key)
    platforms = db.StringListProperty()
    duedate = db.DateProperty()
    
class TestCaseResult(db.Model):
    testrun = db.ReferenceProperty(TestRun)
    
    testsuite = db.ReferenceProperty(TestSuite)
    testcase = db.ReferenceProperty(TestCase)
    summary = db.StringProperty()
    instruction = db.TextProperty()
    expected_result = db.TextProperty()
    
    platform = db.StringProperty()
    closed = db.BooleanProperty(default=False)
    success = db.BooleanProperty()
    remarks = db.TextProperty()
    user = db.UserProperty()
    
class IncrementalID(db.Model):
    count = db.IntegerProperty(required=True, default=0)
    
    @classmethod
    def nextID(cls):
        def txn():
            key = "IncrementalCounter"
            counter = IncrementalID.get_by_key_name(key)
            if counter is None:
                counter = IncrementalID(key_name = key)
            counter.count += 1
            c = counter.count
            counter.put()
            return c
        return db.run_in_transaction(txn)